<template>
  <div class="link-nav">
    <div class="nav-box">
      <div
        :style="{
          color: lineIndex === index ? navColor : '#fff',
        }"
        class="nav-list"
        v-for="(item, index) in navList"
        :key="index"
        @click="getNavListId(index, item.name)"
        ref="navList"
      >
        {{ item.title }}
      </div>

      <div
        class="line"
        :style="{
          width: `${this.lineWidth}px`,
          backgroundColor: lineColor,
          left: `${lineLeft}px`,
          height: actionStyle ? '100%' : '3px',
        }"
      ></div>
    </div>
    <div class="nav-list-slot" ref="navListSlot">
      <slot :name="name"></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    navList: Array,
    lineColor: { type: String, default: 'yellow' },
    navColor: { type: String, default: '#fff' },
    actionStyle: { type: Boolean, default: false }
  },
  data () {
    return {
      lineLeft: 0,
      lineWidth: 0,
      lineIndex: 0,
      name: ''
    }
  },
  methods: {
    getNavListId (e, name) {
      this.lineWidth = this.$refs.navList[0].clientWidth
      this.lineLeft = e * this.lineWidth
      this.lineIndex = e
      this.name = name
      this.$emit('getNavListId', e)
      // this.getNavListSlotChildrenHeight()
    },
    // 主要拿到solt的高度
    async getNavListSlotChildrenHeight () {
      let navListSlotChildrenHeight = 0
      await this.$nextTick(() => {
        const navListSlotChildren = this.$refs.navListSlot.children
        for (let i = 0; i < navListSlotChildren.length; i++) {
          navListSlotChildrenHeight += navListSlotChildren[i].offsetHeight
        }
      })
      if (navListSlotChildrenHeight > window.innerHeight - 50) {
        this.$refs.navListSlot.style.height = window.innerHeight - 50 + 'px'
      } else {
        this.$refs.navListSlot.style.overflowY = 'auto'
      }
    }
  },
  mounted () {
    this.lineWidth = this.$refs.navList[0].clientWidth
    this.name = this.navList[0].name
    // this.getNavListSlotChildrenHeight()
  }
}
</script>

<style scoped>
.nav-box {
  background-color: rgb(224, 136, 136);
  width: 100%;
  height: 50px;
  display: flex;
  position: relative;
}

.nav-list {
  flex: 1;
  text-align: center;
  line-height: 50px;
  color: #fff;
  cursor: pointer;
  z-index: 1;
  position: relative;
}
.line {
  position: absolute;
  transition: all 0.5s;
  bottom: 0px;
  z-index: 0;
}
/* .nav-list-slot {
  overflow-y: scroll;
} */

</style>
